package project.elements.base;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Job {
	private String name;

	private long dueDate;
	private long onsetTime;

	private final Set<Task> tasks;

	// private Object costs;

	public Job(String name, long dueDate, long onsetTime, Task[] tasks) {
		super();
		this.name = name;
		this.dueDate = dueDate;
		this.onsetTime = onsetTime;
		this.tasks = new HashSet<Task>();
		this.tasks.addAll(Arrays.asList(tasks));
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public long getCost() {
		long result = 0;
		for (Task t : this.tasks) {
			result += t.getCost();
		}
		return result;
	}

	public long getDueDate() {
		return dueDate;
	}

	public void setDueDate(int dueDate) {
		this.dueDate = dueDate;
	}

	// public void setCost(long cost) {
	// this.cost = cost;
	// }
	public void setDueDate(long dueDate) {
		this.dueDate = dueDate;
	}

	public long getOnsetTime() {
		return onsetTime;
	}

	public void setOnsetTime(long onsetTime) {
		this.onsetTime = onsetTime;
	}

	public void addTask(Task task) {
		this.tasks.add(task);
	}

	public void removeTask(Task task) {
		this.tasks.remove(task);
	}

	public Task getTaskByMachine(Machine machine) {
		Task result = null;
		for (Task t : this.tasks) {
			if (t.getMachine().equals(machine)) {
				result = t;
				break;
			}
		}
		return result;
	}

	public List<Task> getnextTasks() {
		List<Task> result = new ArrayList<Task>();
		for (Task t : this.tasks) {
			if (t.getStartTime() == -1) {
				result.add(t);
			}
		}
		return result;
	}

	public Set<Task> getTasks() {
		return tasks;
	}

	@Override
	public String toString() {
		return this.name + ";" + this.onsetTime + ";" + this.dueDate + ";" + this.getCost();
	}

	// public int getOperationNumber() {
	// return operationNumber;
	// }
	//
	// public void setOperationNumber(int operationNumber) {
	// this.operationNumber = operationNumber;
	// }

}